//C07:TokenIteratorTest.cpp {-g++}
#include <fstream>
#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>
#include <deque>
#include <set>
#include <cstring>
#include "TokenIterator.h"
#include "../require.h"
using namespace std;

int main(int argc, char* argv[])
{
    char* fname = "TokenIteratorTest.cpp";
    if(argc > 1)
    {
        fname = argv[1];
    }
    ifstream in(fname);
    assure(in, fname);
    ostream_iterator<string> out(cout, "\n");
    typedef istreambuf_iterator<char> IsbIt;
    IsbIt begin(in), isbEnd;
    Delimeters delimeters(" \t\n~:()\"<>:{}[]+-=&*#../\\");
    TokenIterator<IsbIt, Delimeters> 
        wordIter(begin, isbEnd, delimeters), end;
    vector<string> wordlist;
    copy(wordIter, end, back_inserter(wordlist));
    // Output results:
    copy(wordlist.begin(), wordlist.end(), out);
    *out++ = "---------------------";
    // Use a char array as the source:
    char* cp = "typedef std::istreambuf_iterator<char> It";
    TokenIterator<char*, Delimeters>
        charIter(cp, cp + strlen(cp), delimeters), end2;
    vector<string> wordlist2;
    copy(charIter, end2, back_inserter(wordlist2));     
    copy(wordlist2.begin(), wordlist2.end(), out);
    *out++ = "----------------";
    // Use a deque<char> as the source:
    ifstream in2("TokenIteratorTest.cpp");
    deque<char> dc;
    copy(IsbIt(in2), IsbIt(), back_inserter(dc));
    TokenIterator<deque<char>::iterator, Delimeters>
        dcIter(dc.begin(), dc.end(), delimeters), end3;
    vector<string> wordlist3;
    copy(dcIter, end3, back_inserter(wordlist3));
    copy(wordlist3.begin(), wordlist3.end(), out);
    *out++ = "-------------------";
    // Reproduce the Wordlist.cpp example:
    ifstream in3("TokenIterator.cpp");
    TokenIterator<IsbIt, Delimeters>
        wordIter2(IsbIt(in3), isbEnd, delimeters);
    set<string> wordlist4;
    while(wordIter2 != end)    
    {
        wordlist4.insert(*wordIter2++);
    }    
    copy(wordlist4.begin(), wordlist4.end(), out);
}